<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="dialogVisible = true"
          >添加分类</el-button
        >
      </div>
      <el-table stripe border :data="list" style="width: 100%">
        <el-table-column label="序号" type="index"> </el-table-column>
        <el-table-column label="分类名称" prop="cate_name"> </el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="getListItem(scope.row)" type="primary">编辑</el-button>
            <el-button size="mini" type="danger" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog  @closed="$refs.cateRef.resetFields()" title="添加文章分类" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="cateForm"
        :rules="cateRules"
        ref="cateRef"
        label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="cateForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="cateForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog  @closed="$refs.updateRef.resetFields()" title="修改文章分类" :visible.sync="dialogVisible1" width="30%">
      <el-form
        :model="updateForm"
        :rules="cateRules"
        ref="updateRef"
        label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="updateForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="updateForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible1 = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="updateCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, add, info, delCate } from 'network/login'

export default {
  name: 'ArtCate',
  data () {
    return {
      // 文章分类列表数据
      list: [],
      dialogVisible: false,
      dialogVisible1: false,
      updateForm: {},
      cateForm: {
        cate_name: '',
        cate_alias: '',
        content: ''
      },
      cateRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '请输入1-10位的非空格分类名称', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[0-9a-zA-Z]{1,15}$/, message: '请输入1-15位字母数字的分类别名', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    // 组件创建立即获取文章分类列表
    this.getList()
  },
  methods: {
    async getList () {
      // 发起网络请求获取文章分类列表
      const { data: res } = await list()
      this.list = res.data
    },
    // 添加文章
    addCate () {
      this.$refs.cateRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await add(this.cateForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.getList()
        this.dialogVisible = false
      })
    },
    // 渲染编辑的表单
    getListItem (row) {
      if (row.id === 1 || row.id === 2) return this.$message.error('管理员不允许修改')
      this.updateForm = { ...row }
      this.dialogVisible1 = true
    },
    // 更新要修改的表单
    updateCate () {
      this.$refs.updateRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await info(this.updateForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.getList()
        this.dialogVisible1 = false
      })
    },
    // 删除文章
    del (id) {
      this.$confirm('确定要删除文章吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await delCate({ id })
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.getList()
      }).catch(() => {})
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-dialog__body {
  padding: 20px 20px 8px;
}
</style>
